package o2o.config.jms.convert;

import com.jinyouapp.common.tools.jms.model.JMSNotify;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

/**
 * 类名称：NotifyMessageConverter
 * 类描述：消息转换
 * 创建人：Jahnke 1029777564@qq.com
 * 创建时间：2016年8月10日 下午9:32:14
 * 修改备注：
 */
@Component
public class NotifyMessageConverter implements MessageConverter {
    private static Logger logger = LoggerFactory
            .getLogger(NotifyMessageConverter.class);

    /**
     * 转换接收到的消息为NoticeInfo对象
     */
    public Object fromMessage(Message message) throws JMSException,
            MessageConversionException {
        if (logger.isDebugEnabled()) {
            logger.debug("Receive JMS message :" + message);
        }
        if (message instanceof ObjectMessage) {
            ObjectMessage oMsg = (ObjectMessage) message;
            if (oMsg instanceof ActiveMQObjectMessage) {
                ActiveMQObjectMessage aMsg = (ActiveMQObjectMessage) oMsg;
                try {
                    JMSNotify noticeInfo = (JMSNotify) aMsg.getObject();
                    return noticeInfo;
                } catch (Exception e) {
                    logger.error("Message:${} is not a instance of NoticeInfo."
                            + message.toString());
                    throw new JMSException("Message:" + message.toString()
                            + "is not a instance of NoticeInfo."
                            + message.toString());
                }
            } else {
                logger.error("Message:${} is not a instance of ActiveMQObjectMessage."
                        + message.toString());
                throw new JMSException("Message:" + message.toString()
                        + "is not a instance of ActiveMQObjectMessage."
                        + message.toString());
            }
        } else {
            logger.error("Message:${} is not a instance of ObjectMessage."
                    + message.toString());
            throw new JMSException("Message:" + message.toString()
                    + "is not a instance of ObjectMessage."
                    + message.toString());
        }
    }

    /**
     * 转换NoticeInfo对象到消息
     */
    public Message toMessage(Object obj, Session session) throws JMSException,
            MessageConversionException {
        if (logger.isDebugEnabled()) {
            logger.debug("Convert Notify object to JMS message:${}"
                    + obj.toString());
        }
        if (obj instanceof JMSNotify) {
            ActiveMQObjectMessage msg = (ActiveMQObjectMessage) session
                    .createObjectMessage();
            msg.setObject((JMSNotify) obj);
            return msg;
        } else {
            logger.debug("Convert Notify object to JMS message:${}"
                    + obj.toString());
        }
        return null;
    }
}
